'''
Created on 14/10/2009

@author: Tony Allan
'''
from runtime import rt
from services import *
from services.httpserver import * # FIXME: not sure why I need this
import unittest

lock = threading.Lock()

class DrivingTask(Task):
    
    ''' a user of htmltask and httptask '''
    def cb_start(self, method, uri, res, result, context, **kwargs):
        #self.log('DrivingTask(cb_start): %s' % uri)
        html_uri = self.context.uri('html:').template.basic.collection()
        
        # create some test data
        data = Resource()
        data.set_type(context.uri('services:resource/htmltestdata'))
        data.hello = 'World!'
        data.test = ['aaa', {'bbb': ['ccc', 'ddd'], 'eee': 'fff', 'ggg': 'hhh'}, 'iii']
        generated_html_uri = self.context.put(html_uri, data)    # HTML generated when this returns
        self.gen_res = self.context.remove(generated_html_uri)        # get then delete the generated html
        self.log('DrivingTask(cb_start): Content=%s' % self.gen_res.content)
        # setup sample data for the web server at http://localhost:8000/sample/sample1.html
        url = context.uri('local:').sample.add('sample1.html')
        self.log('Sample: %s' % url.full())
        context.put(url, self.gen_res)
        # add a new HTTP site
        res = Resource()
        http_uri = self.context.uri('tasks:').base.service.httptask
        server_uri = self.context.uri('local:')
        res.listen = server_uri
        self.log('Adding site %s' % res.listen)
        self.context.put(http_uri.listener.local, res)
        # release this lock as the driver task is done
        lock.release()

class HTMLTaskTest(unittest.TestCase):

    def test_html_and_http(self):
        '''[htmltask] Test htmltask using httptask. '''
        # setup for tests
        queue = Queue()
        spaces = Spaces()
        context = SpacesContext(spaces, queue)
        runtime = rt.Runtime()
        runtime.initialise_spaces_with_local_config_files(context)
        # dummy task def resource
        task_def_resource = Resource()                    
        #
        # start the html task
        taskh_uri = context.uri('tasks:').base.service.htmltask
        taskh = HTMLTask(spaces, taskh_uri, task_def_resource)
        taskh.task_start()
        #
        # start the http task
        taska_uri = context.uri('tasks:').base.service.httptask
        taska = HTTPTask(spaces, taska_uri, task_def_resource)
        taska.task_start()
        #        
        # start the test driver
        time.sleep(0)
        taskd_uri = context.uri('tasks:').base.service.htmltasktest
        taskd = DrivingTask(spaces, taskd_uri, task_def_resource)
        taskd.task_start()
        #
        # lock until the driver task finishes
        lock.acquire()
        lock.acquire()
        lock.release()
        #        
        time.sleep(4)
        # 4 seconds is give to now process the HTTP request
        # Note: In this example the same resource is returned over and over again 
        # because HTML is only generated once. Not sure the best way to fix this
        # other than process or process_all.        
        #
        taskh.task_stop()
        taska.task_stop()
        taskd.task_stop()
        time.sleep(1)

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()